/**
 * 
 */
package org.alipay4j;

import org.alipay4j.loader.ITokenLoader;

/**
 * 支付宝对象构造器
 * @author 贺亮
 *
 */
public class AlipayBuilder {
	private Alipay alipay;
	private ITokenLoader tokenLoader;
	
	/**
     * 获取一个新的生活号构建器
     *
     * @return 生活号构造器
     */
    public static AlipayBuilder newInstance() {
    	AlipayBuilder builder = new AlipayBuilder();
        builder.alipay = new Alipay();
        return builder;
    }
    
    /**
     * 外部自定义生活号配置
     *
     * @param alipayConfig 生活号配置对象
     * @return 自身引用对象
     * @since 0.1.3
     */
    public static AlipayBuilder newInstance(AlipayConfig alipayConfig) {
    	AlipayBuilder builder = new AlipayBuilder();
        builder.alipay = new Alipay(alipayConfig);
        return builder;
    }
    
    /**
     * 外部自定义支付配置
     *
     * @param payConfig 支付配置对象
     * @return 自身引用对象
     * @since 0.1.3
     */
    public static AlipayBuilder newInstance(AlipayPayConfig payConfig) {
    	AlipayBuilder builder = new AlipayBuilder();
        builder.alipay = new Alipay(payConfig);
        return builder;
    }
    
    /**
     * 外部生活号+自定义支付配置
     * @param alipayConfig 生活号对象
     * @param payConfig 支付配置对象
     * @return 自身引用对象
     * @since 0.1.3
     */
    public static AlipayBuilder newInstance(AlipayConfig alipayConfig,AlipayPayConfig payConfig) {
    	AlipayBuilder builder = new AlipayBuilder();
        builder.alipay = new Alipay(alipayConfig,payConfig);
        return builder;
    }
    
    
    /**
     * 配置token加载器
     *
     * @param tokenLoader token加载器
     * @return return this
     */
    public AlipayBuilder setTokenLoader(ITokenLoader tokenLoader) {
        if (tokenLoader == null) {
            throw new IllegalStateException("tokenLoader can't be null");
        }
        this.tokenLoader = tokenLoader;
        return this;
    }
    
    
    /**
     * 返回最终配置好的Alipay对象
     *
     * @return 微信对象
     */
    public Alipay build() {
        if (tokenLoader != null) {
            alipay.tokenLoader = this.tokenLoader;
        }
        return alipay;
    }
    
    
}
